﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZhengShouFanWeiDongJieControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            if (!base.Page.IsPostBack) {
                Bind();
            }
            Button btnsearch = (Button)FindControl("btnsearch");
            btnsearch.Click += new EventHandler(Go_Search);
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            GridList.OnLoadData += new EventHandler(GridList_OnLoadData);
            foreach (GridViewRow gvr in GridList.Rows)
            {
                Button btn = (Button)gvr.FindControl("btn_tj");
                btn.Click += new EventHandler(ChangeStatus);
                //Button btn1 = (Button)gvr.FindControl("btn_shtg");//审核通过
                //btn1.Click += new EventHandler(ChangeStatus1);
                Button btn2 = (Button)gvr.FindControl("btn_tj2");
                btn2.Click += new EventHandler(ChangeStatus2);
                //Button btn_dzqc = (Button)gvr.FindControl("btn_dzqc");
                //btn_dzqc.Click += new EventHandler(btn_dzqc_Click);

                Button btnCs = (Button)gvr.FindControl("btnCs");
                btnCs.Click += new EventHandler(ChangeStatusSh);
                Button btnCh = (Button)gvr.FindControl("btnCh");
                btnCh.Click += new EventHandler(ChangeStatusSh);
                Button btnFs = (Button)gvr.FindControl("btnFs");
                btnFs.Click += new EventHandler(ChangeStatusSh);
                Button btnHs = (Button)gvr.FindControl("btnHs");
                btnHs.Click += new EventHandler(ChangeStatusSh);
                
            }
            string jsstrIP = base.Page.Request.ServerVariables["HTTP_HOST"];

            //判断是否为管理员，如果是则功能均可见，但不一定能操作
            if (!_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid))
            {
                //判断是否是内网用户，如果是代表是征收办人员
                if (jsstrIP == System.Configuration.ConfigurationManager.AppSettings["FCSERVER"])
                {
                    GridList.Columns[9].Visible = true;//审核
                }
                else
                {
                    GridList.Columns[9].Visible = false;//审核
                }

                //如果是市征收角色的用户（非实施单位用户）
                if (_EluZSDataProvider.IsSZSB(userid))
                {
                    GridList.Columns[9].Visible = false;//审核
                    GridList.Columns[10].Visible = false;//撤销
                    GridList.Columns[7].Visible = false;//编辑
                    GridList.Columns[8].Visible = false;//提交
                }
            }

        }

    
        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }

        protected void Bind()
        {
            
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            GridList.RowCreated += new GridViewRowEventHandler(GridList_RowCreated);
            GridList.RowDataBound += new GridViewRowEventHandler(GridList_RowDataBound);
            List<EluZSProvider.Project> project = _EluZSDataProvider.GetProject(GetSqlWhere());
            GridList.DataSource = project;
            GridList.RecordCount = project.Count;
            GridList.PageSize = 10;
            GridList.DataKeyNames = new string[] { "ID","立项号", "冻结状态", "冻结审批状态", "提醒开户状态", "审批状态" };
            GridList.DataBind();
        }
        protected void GridList_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        protected void GridList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            foreach (GridViewRow gvr in GridList.Rows)
            {
                if (GridList.DataKeys[gvr.RowIndex].Values["冻结状态"].ToString() == "0")
                {
                    Button btn = (Button)gvr.FindControl("btn_tj");
                    btn.Enabled = true;
                }
                else
                {
                    Button btn2 = (Button)gvr.FindControl("btn_tj2");
                    btn2.Enabled = true;
                }
                
                if (GridList.DataKeys[gvr.RowIndex].Values["冻结审批状态"].ToString() == "1")
                {
                    Button btn = (Button)gvr.FindControl("btn_tj2");
                    btn.Enabled = false;
                }
            }

        }
        protected void GridList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                EluZSProvider.Project rowview = (EluZSProvider.Project)e.Row.DataItem;
                string temp = rowview.提交状态.ToString();
                string strPID = rowview.Id.ToString();
                string strTJ = rowview.冻结状态.ToString();
                string strSHTG = rowview.冻结审批状态.ToString();
                //Button btn1 = (Button)e.Row.FindControl("btn_shtg");
                //if (strTJ == "1") { btn1.Enabled = true; }
                //if (strSHTG == "1") { btn1.Enabled = false; }

                Button btnCs = (Button)e.Row.FindControl("btnCs");
                Button btnCh = (Button)e.Row.FindControl("btnCh");
                Button btnFs = (Button)e.Row.FindControl("btnFs");
                Button btnHs = (Button)e.Row.FindControl("btnHs");

                if (strTJ == "1" && strSHTG == "0") { btnCs.Enabled = true; }
                if (strSHTG == "-3") { btnCh.Enabled = true; }
                if (strSHTG == "-2") { btnFs.Enabled = true; }
                if (strSHTG == "-1") { btnHs.Enabled = true; }
                
                if (temp != "1")
                {
                    e.Row.Cells[7].Enabled = true;
                    e.Row.Cells[7].Text = "编辑";

                    //e.Row.Cells[8].Enabled = true;
                    //e.Row.Cells[8].Text = "查看";

                    //e.Row.Cells[9].Enabled = true;
                    //e.Row.Cells[9].Text = "查看";

                    e.Row.Cells[8].Enabled = false;
                    e.Row.Cells[9].Enabled = false;
                }
                //if (_EluZSDataProvider.IsQZSBM(userid))
                //{
                //    e.Row.Cells[11].Visible = false;
                //}
                //string jsstrIP =HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
                //if (!_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid))
                //{
                //    if (jsstrIP == System.Configuration.ConfigurationManager.AppSettings["FCSERVER"])
                //    {
                //        GridList.Columns[9].Visible = false;
                //    }
                //    else
                //    {
                //        if (!_EluZSDataProvider.IsSZSB(userid))
                //        {
                //            GridList.Columns[9].Visible = true;
                //        }
                //        //else
                //        //{
                //        //    if (!_EluZSDataProvider.IsSQLXPro(strPID))
                //        //    {
                //        //        btn1.Visible = true;
                //        //    }
                //        //    else
                //        //    {
                //        //        btn1.Visible = true;
                //        //    }
                //        //}
                //    }
                //}
            }
        }
        /// <summary>
        /// 修改提交状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ChangeStatus(object sender, EventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string pID = GridList.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool change = _EluZSDataProvider.GetProjectZT("ZS_PROJECT", "冻结状态", pID, "1");
            if (change) { Bind(); _EluZSDataProvider.InsertZSFW(pID, userid, "1", "范围冻结"); }
        }

        /// <summary>
        /// 修改审核通过状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ChangeStatusSh(object sender, EventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string pID = GridList.DataKeys[gvr.RowIndex].Values["ID"].ToString();
           
            string strScript = string.Format("<script>OpenWin('{0}')</script>", pID);
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "regLxsqYj", strScript);
        }

        /// <summary>
        /// 修改审核通过状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ChangeStatus1(object sender, EventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string pID = GridList.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool change = _EluZSDataProvider.GetProjectZT("ZS_PROJECT", "冻结审批状态", pID, "1");
            if (change) { Bind(); }
        }

        /// <summary>
        /// 撤销提交状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ChangeStatus2(object sender, EventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string pID = GridList.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool change = _EluZSDataProvider.GetProjectZT("ZS_PROJECT", "冻结状态", pID, "0");
            if (change) { Bind(); }
        }


        protected void btn_dzqc_Click(object sender, EventArgs e)
        {

            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string lxh = GridList.DataKeys[gvr.RowIndex].Values["立项号"].ToString();
            Page.Response.Redirect("..//BCSSManage//ProjectDZQC.aspx?lxh="+lxh);

        }


        protected string GetSqlWhere() {
            string sqlWhere = string.Empty;
            TextBox xmmc = (TextBox)FindControl("xmmc");
            TextBox lxh = (TextBox)FindControl("lxh");
            TextBox xkzh = (TextBox)FindControl("xkzh");
            TextBox zsssdw = (TextBox)FindControl("zsssdw");
            if (!string.IsNullOrEmpty(xmmc.Text.Trim()))
            {
                sqlWhere += " and 项目名称  like '%" + xmmc.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(lxh.Text.Trim()))
            {
                sqlWhere += " and 立项号 like '%" + lxh.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(xkzh.Text.Trim()))
            {
                sqlWhere += " and id in(select pid from zs_prono where 征收许可证号 like '%" + xkzh.Text.Trim() + "%')";
            }
            if (!string.IsNullOrEmpty(zsssdw.Text.Trim()))
            {
                sqlWhere += " and 征收实施单位 like '%" + zsssdw.Text.Trim() + "%'";
            }
//            if (_EluZSDataProvider.IsQZSBM(userid))
//            {
//                sqlWhere += @" and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
//Where  t.userid Like '%" + userid + "%') Like '%'||区属||'%' ";
//            }
            if (_EluZSDataProvider.IsQZSBM(userid))
            {
                sqlWhere += @" and (Select max(Rolename) Rolename FROM (SELECT sys_connect_by_path(Rolename,',') Rolename FROM (SELECT Rolename,rn rchild,(rn-1) rfather FROM (SELECT   t.Rolename,row_number() over (ORDER BY t.Rolename) rn FROM (Select Tt.Rolename From Zs_User_In_Role t Join Zs_Role Tt On t.Roleid = Tt.Id Where t.Userid Like '%" + userid + "%') t)) CONNECT BY PRIOR rchild=rfather START WITH rfather LIKE '%0')) Like '%' || substr(区属,0,2) || '%'";
            }
            sqlWhere += " order by addtime desc";
            return sqlWhere;
        }
    }
}


